HasOneThrough   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
A getRelationalFields 0 3 5
A setName 0 8 1
1
import Relation from "../Relation";
2
import {ModelStaticInterface} from "../../../JeloquentInterfaces";
3
import ForeignKey from "../Field/ForeignKey";
4
5
export default class HasOneThrough extends Relation {
6
7
    localKey: string;
8
9
    throughModel: ModelStaticInterface;
10
11
    private _lcModelClassName: string;
12
13
    private _lcParentClassName: string;
14
15
    private _lcThroughModelClassName: string;
16
17
    constructor(model: ModelStaticInterface, throughModel: ModelStaticInterface, foreignKey: string = null, localKey = 'id') {
18
        super(model, foreignKey);
19
        this.throughModel = throughModel;
20
        this.localKey = localKey;
21
    }
22
23
    get indexName(): string {
24
        return `${this._lcThroughModelClassName}.${this._lcParentClassName}_id`;
25
    }
26
27
    get originalValue(): unknown {
28
        const findModel = this.$parent[`original_${this._lcThroughModelClassName}`];
29
        return findModel[`original_${this._lcModelClassName}`] ?? null;
30
    }
31
32
    get value(): unknown {
33
        const findModel = this.$parent[this._lcThroughModelClassName];
34
        return findModel[this._lcModelClassName] ?? null;
35
    }
36
37
    getRelationalFields(): Array<ForeignKey> {
38
        return [];
39
    }
40
41
    setName(): HasOneThrough {
42
        this._lcThroughModelClassName = this.throughModel.snakeCaseClassName;
43
        this._lcModelClassName = this.model.snakeCaseClassName;
44
        this._lcParentClassName = this.$parent.snakeCaseClassName;
45
        this.foreignKey = `${this._lcThroughModelClassName}_id`;
46
        this.$name = `${this._lcModelClassName}`;
47
        return this;
48
    }
49
}